package com.mazaiting.livedetection

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.abcd.R
import com.facebook.react.bridge.*
import com.facebook.react.module.annotations.ReactModule
import com.sensetime.liveness.motion.MotionLivenessActivity
import com.sensetime.senseid.sdk.liveness.interactive.common.app.AbstractCameraActivity
import java.io.File


/**定义常量， 活体检测请求码*/
const val REQUEST_LIVE_DETECTION = 0x100

/**
 * 活体检测
 * Created by mazaiting on 2018/7/18.
 */
@ReactModule(name = "LiveDetectionModule")
class LiveDetectionModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
  private val TAG = LiveDetectionModule::class.java.simpleName
  /**
   * Activity结果返回监听
   */
  private val activityEventListener = object : BaseActivityEventListener() {
    override fun onActivityResult(activity: Activity?, requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(activity, requestCode, resultCode, data)
      if (Activity.RESULT_OK == resultCode) {
        // 活体检测
        if (REQUEST_LIVE_DETECTION == requestCode) {
          Toast.makeText(currentActivity, "检测成功", Toast.LENGTH_SHORT).show()
          val imgFile = File(MotionLivenessActivity.RESULT_PATH)
          // 判断文件是否存在
          if (!imgFile.exists() && imgFile.list() == null) return
          // 获取文件列表
          val files = imgFile.list()
          // 图片文件列表
          val imgs = arrayListOf<String>()
          for (fileName in files) {
            // 获取文件名'.'的位置
            val index = fileName.lastIndexOf('.')
            if (index > 0 && "jpg" == fileName.substring(index + 1)) {
              Log.e(TAG, fileName)
              imgs.add(fileName)
            }
          }
          
        }
      } else {
        // 显示错误信息
        val msg = showErrorMessage(resultCode)
        Toast.makeText(currentActivity, msg, Toast.LENGTH_SHORT).show()
      }
    }
  }
  
  /**
   * 初始化
   */
  init {
    reactContext.addActivityEventListener(activityEventListener)
  }
  
  override fun getName(): String {
    return "LiveDetectionModule"
  }
  
  /**
   * 开启活体检测界面
   */
  @ReactMethod
  fun startMotionLivenessActivity() {
    val intent = Intent(currentActivity, MotionLivenessActivity::class.java)
    // 设置难易程度 -- EASY: 0, NORMAL: 1, HARD: 2, HELL: 3
    intent.putExtra(MotionLivenessActivity.EXTRA_DIFFICULTY, 1)
    // 设置是否开启声音 true: 开启, false: 不开启
    intent.putExtra(MotionLivenessActivity.EXTRA_VOICE, true)
    // 设置顺序 [0, 1, 3, 2]
    val sequences = intArrayOf(0, 1, 2, 3)
    intent.putExtra(MotionLivenessActivity.EXTRA_SEQUENCES, sequences)
    currentActivity!!.startActivityForResult(intent, REQUEST_LIVE_DETECTION)
  }
  
  /**
   * 显示错误信息
   */
  private fun showErrorMessage(resultCode: Int): String {
    val messageCode: Int
    when (resultCode) {
      AbstractCameraActivity.RESULT_CANCELED -> messageCode = R.string.txt_error_canceled
      AbstractCameraActivity.RESULT_CODE_CAMERA_ERROR -> messageCode = R.string.txt_error_camera
      AbstractCameraActivity.RESULT_CODE_NO_PERMISSIONS -> messageCode = R.string.txt_error_permission
      AbstractCameraActivity.RESULT_CODE_TIMEOUT -> messageCode = R.string.txt_error_timeout
      AbstractCameraActivity.RESULT_CODE_MODEL_FILE_NOT_FOUND -> messageCode = R.string.txt_error_model_not_found
      AbstractCameraActivity.RESULT_CODE_LICENSE_FILE_NOT_FOUND -> messageCode = R.string.txt_error_license_not_found
      AbstractCameraActivity.RESULT_CODE_CHECK_LICENSE_FAIL, AbstractCameraActivity.RESULT_CODE_SDK_VERSION_MISMATCH, AbstractCameraActivity.RESULT_CODE_PLATFORM_NOTSUPPORTED -> messageCode = R.string.txt_error_license
      AbstractCameraActivity.RESULT_CODE_CHECK_MODEL_FAIL -> messageCode = R.string.txt_error_model
      AbstractCameraActivity.RESULT_CODE_LICENSE_EXPIRE -> messageCode = R.string.txt_error_license_expire
      AbstractCameraActivity.RESULT_CODE_LICENSE_PACKAGE_NAME_MISMATCH -> messageCode = R.string.txt_error_license_package_name
      AbstractCameraActivity.RESULT_CODE_WRONG_STATE -> messageCode = R.string.txt_error_state
      AbstractCameraActivity.RESULT_CODE_HACK -> messageCode = R.string.txt_error_action_fail
      AbstractCameraActivity.RESULT_CODE_FACE_STATE -> messageCode = R.string.txt_error_action_over
      AbstractCameraActivity.RESULT_CODE_FACE_COVERED -> messageCode = R.string.txt_error_face_cover_detecting
      AbstractCameraActivity.RESULT_CODE_NETWORK_TIMEOUT -> messageCode = R.string.txt_error_server_timeout
      AbstractCameraActivity.RESULT_CODE_ERROR_API_KEY_SECRET -> messageCode = R.string.error_api_key_secret
      AbstractCameraActivity.RESULT_CODE_SERVER -> messageCode = R.string.error_server
      AbstractCameraActivity.RESULT_CODE_INVALID_ARGUMENTS -> messageCode = R.string.invalid_arguments
      else -> messageCode = -1
    }
    return if (messageCode != -1) currentActivity!!.getString(messageCode) else "检测失败"
  }
  
}